package com.fishingwithme.android.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.navigation.NavController
import com.fishingwithme.android.navigation.FishingNavigation
import com.fishingwithme.android.ui.screens.my.MyScreenHiltEntryPoint
import com.fishingwithme.android.ui.theme.AppDimensions
import dagger.hilt.android.EntryPointAccessors

@Composable
fun EditDrawer(
    isVisible: Boolean,
    onDismiss: () -> Unit,
    navController: NavController
) {
    val context = LocalContext.current
    val preferencesManager = EntryPointAccessors.fromApplication(
        context.applicationContext,
        MyScreenHiltEntryPoint::class.java
    ).preferencesManager()

    // Get user data
    val user by preferencesManager.getLoginUserObj().collectAsState(initial = null)
    if (isVisible) {
        Dialog(
            onDismissRequest = onDismiss,

            properties = DialogProperties(
                usePlatformDefaultWidth = false,
                dismissOnClickOutside = true,
                dismissOnBackPress = true,

                )
        ) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.Transparent),
                contentAlignment = Alignment.BottomCenter
            ) {
                // 半透明背景层
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .clickable(onClick = onDismiss)
                    //.background(Color.Black.copy(alpha = 0.5f))
                )

                // 抽屉内容
                Surface(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(MaterialTheme.colorScheme.surface)
                ) {
                    Column(
                        modifier = Modifier.padding(AppDimensions.SPACING_16),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        // 按钮列表
                        Column(
                            verticalArrangement = Arrangement.spacedBy(AppDimensions.SPACING_2),
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            // 添加钓点按钮
                            DrawerButton(
                                label = "添加钓点",
                                isPrimary = false,
                                onClick = {
                                    onDismiss()
                                    navController.navigate(FishingNavigation.ADD_SPOT)
                                }
                            )

                            // 晒鱼获按钮
                            DrawerButton(
                                label = "晒鱼获",
                                isPrimary = false,
                                onClick = {
                                    onDismiss()
                                    navController.navigate(FishingNavigation.HARVEST)
                                }
                            )

                            // 添加水域按钮
                            if(user?.roles?.contains("ROLE_CIRCLE_MAINTAINER") ?: false ) {
                                DrawerButton(
                                    label = "添加水域",
                                    isPrimary = false,
                                    onClick = {
                                        onDismiss()
                                        navController.navigate(FishingNavigation.ADD_CIRCLE)
                                    }
                                )
                            }

                            // 取消按钮
                            DrawerButton(
                                label = "取消",
                                isPrimary = false,
                                onClick = onDismiss
                            )
                        }
                    }
                }
            }
        }
    }
}

@Composable
private fun DrawerButton(
    label: String,
    isPrimary: Boolean,
    onClick: () -> Unit
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .background(if (isPrimary) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant)
            .padding(AppDimensions.SPACING_12)
    ) {
        Text(
            text = label,
            style = MaterialTheme.typography.bodyLarge,
            color = if (isPrimary) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurfaceVariant,
            modifier = Modifier.align(Alignment.Center)
        )
    }
}